<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use app\admin\model\Comment;
use app\common\model\Category as CategoryModel;
/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['banner','category','goods_list','goods_detail','get_child','get_rent_catelist','rent_good_list','search'];
    protected $noNeedRight = ['*'];
    protected $LitestoreGoods = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->user_id = $this->auth->id;
        // $this->user_id = 2;
        $this->LitestoreGoods = new \app\admin\model\LitestoreGoods;
    }  


    /**
     * 不同分区不同轮播图
     * @ApiSummary  ()
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/index/banner)
     * @ApiParams   (name="type",type=string,required=true,description="传分区过来")
     * @ApiReturn   ({
        
        })
     */
      public function banner(){
        $return = Db::name('banner')->where(['status'=>input('type')])->order('weigh desc')->select();
        $this->success('不同分区不同轮播图',$return);
      }


    /**
     * 分类列表
     * @ApiSummary  (最多有四个级别)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/index/category)
     * @ApiParams   (name="type",type=string,required=true,description="传分区过来")
     * @ApiReturn   ({
        
        })
    
     */
    public function category(){
        
        $return = Db::name('category')->where(['type'=>input('type'),'flag'=>'Default','pid'=>0])->field('name,id')->order('weigh desc')->select();
        foreach ($return as $key => $value) {
            $return[$key]['child_list'] = Db::name('category')->where(['pid'=>$value['id']])->field('name,id')->order('weigh desc')->select();

            if(!empty($return[$key]['child_list'])){
              foreach ($return[$key]['child_list'] as $k => $v) {
                $cc_list = Db::name('category')->where(['pid'=>$v['id']])->field('name,id')->order('weigh desc')->select();
                
                if($cc_list){
                  $return[$key]['child_list'][$k]['cc_list'] = $cc_list;

                  foreach ($cc_list as $kk => $vv) {
                    $ccc_list = Db::name('category')->where(['pid'=>$vv['id']])->field('name,id')->order('weigh desc')->select();
                    if($ccc_list){
                      $return[$key]['child_list'][$k]['cc_list'][$kk] = $ccc_list;
                    }
                  }
                }

              }
            }
            
        }
        $this->success('分类列表',$return);
    }

     /**
     * 商品列表
     * @ApiSummary  (hot：热门，latest：最新，normal：日常，discount：优惠，featured：精选)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/index/goods_list)
     * @ApiParams   (name="category_id",type=int,required=true,description="分类id")
     * @ApiReturn ({
       
     })
     */
    public function goods_list(){
        $category_id = $this->request->request('category_id');
        if(empty($category_id))$this->error('缺少分类id');
        
        $data['hot'] = Db::query("select goods_id,cover,goods_name,introduce from fa_litestore_goods where `good_status` =1 and find_in_set('hot',`show`) and pid=$category_id  order by weigh desc");
        $data['latest'] = Db::query("select goods_id,cover,goods_name,introduce from fa_litestore_goods where `good_status` =1 and find_in_set('latest',`show`) and pid=$category_id order by weigh desc");
        $data['normal'] = Db::query("select goods_id,cover,goods_name,introduce from fa_litestore_goods where `good_status` =1 and find_in_set('normal',`show`) and pid=$category_id  order by weigh desc");
        $data['discount'] = Db::query("select goods_id,cover,goods_name,introduce from fa_litestore_goods where `good_status` =1 and find_in_set('discount',`show`) and pid=$category_id  order by weigh desc");
        $data['featured'] = Db::query("select goods_id,cover,goods_name,introduce from fa_litestore_goods where `good_status` =1 and find_in_set('featured',`show`) and pid=$category_id order by weigh desc");

        $this->success('商品列表',$data);
    }

     /**
     * 搜索商品
     * @param string $type 分区
     * @param string $goods_name 
     */
    public function search(){
        $type = input('type');
        $goods_name = input('goods_name');
            
        $data['hot'] = Db::query("select goods_id,cover,goods_name,introduce from fa_litestore_goods where `good_status` =1 and find_in_set('hot',`show`) and goods_name LIKE '%".$goods_name."%'  order by weigh desc");
        $data['latest'] = Db::query("select goods_id,cover,goods_name,introduce from fa_litestore_goods where `good_status` =1 and find_in_set('latest',`show`) and goods_name LIKE '%".$goods_name."%'  order by weigh desc");
        $data['normal'] = Db::query("select goods_id,cover,goods_name,introduce from fa_litestore_goods where `good_status` =1 and find_in_set('normal',`show`) and goods_name LIKE '%".$goods_name."%' order by weigh desc");
        $data['discount'] = Db::query("select goods_id,cover,goods_name,introduce from fa_litestore_goods where `good_status` =1 and find_in_set('discount',`show`) and goods_name LIKE '%".$goods_name."%'   order by weigh desc");
        $data['featured'] = Db::query("select goods_id,cover,goods_name,introduce from fa_litestore_goods where `good_status` =1 and find_in_set('featured',`show`) and goods_name LIKE '%".$goods_name."%'  order by weigh desc");    

        $this->success('搜索商品',$data);
    }

     /**
     * 商品详情页面
     * @ApiSummary  (spec_type 20:多规格/10:单规格。)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/index/goods_detail)
     * @ApiParams   (name="goods_id",type=int,required=true,description="商品id")
     * @ApiReturn ({
       
     })
     */
    public function goods_detail(){
        $goods_id = $this->request->request('goods_id');
        if(empty($goods_id))$this->error('缺少商品id');
        
        $LitestoreGoods = new LitestoreGoods;
        $detail = $LitestoreGoods->detail($goods_id, ['specRel', 'spec', 'spec_rel.spec']);
        if (!$detail)$this->error('很抱歉，商品信息不存在');
        // 规格信息
        $specData= $LitestoreGoods->getApiManySpecData($detail['spec_rel'], $detail['spec']);

        $detail = $detail->toArray();
        if($detail['spec_type']=='20'){
          unset($detail['spec_rel']);
          // unset($detail['spec']);
       
          $data = array_merge($detail,$specData);
        }else{

          $data = $detail;
        }
        
        $this->success('商品详情页面',$data);

    }

    protected function get_child($data){
      foreach ($data as $key => $value) {

        $if_exist = Db::name('category')->where(['pid'=>$value['id']])->field('name,id')->order('weigh desc')->select();
        if($if_exist){
          $data[$key]['child_list'] = $if_exist;
        }
      }
      return $data;
    }

    /**
    * 获取出租的分类一般就两个级别(Second_hand_shop:二手铺,Rental_house:出租屋,Handcraft:手作坊)
    * @param int $type 传分区过来
    **/
    public function get_rent_catelist(){
        $Second_hand_shop = Db::name('category')->where(['type'=>input('type'),'flag'=>'Second_hand_shop','pid'=>0])->field('name,id')->order('weigh desc')->select();

        $Rental_house = Db::name('category')->where(['type'=>input('type'),'flag'=>'Rental_house','pid'=>0])->field('name,id')->order('weigh desc')->select();
        $Handcraft = Db::name('category')->where(['type'=>input('type'),'flag'=>'Handcraft','pid'=>0])->field('name,id')->order('weigh desc')->select();


        $return['Second_hand_shop'] = $this->get_child($Second_hand_shop);
        $return['Rental_house'] = $this->get_child($Rental_house);
        $return['Handcraft'] = $this->get_child($Handcraft);
        
        $this->success('分类列表',$return);
    }

    /**
     * 出租屋发布
     * @ApiSummary  ()
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/index/good_post)
     * @ApiParams   (name="cover",type=string,required=true,description="商品封面")
     * @ApiParams   (name="images",type=string,required=false,description="商品详情轮播图，多个情况下用逗号隔开")
     * @ApiParams   (name="type",type=string,required=true,description="传分区过来")
     * @ApiParams   (name="flag",type=string,required=true,description="Second_hand_shop:二手铺,Rental_house:出租屋,Handcraft:手作坊")
     * @ApiParams   (name="pid",type=int,required=true,description="分类id")
     * @ApiParams   (name="introduce",type=string,required=false,description="简介")
     * @ApiParams   (name="goods_name",type=string,required=false,description="名称")
     * @ApiParams   (name="fee",type=decimal,required=false,description="运费")
     * @ApiParams   (name="goods_price",type=decimal,required=false,description="价格")
     * @ApiReturn ({
        
     })
     */
    public function good_post(){
        
        $params = input();
        $params['user_id'] = $this->user_id;
        $params['spec_type'] = '10';
        $params['show_money'] = input('goods_price');
        unset($params['goods_price']);
        $spec_a['goods_price'] = input('goods_price');

        //成功之后 存储商品规格
        $spec_many_params = '';

        $result = $this->LitestoreGoods->allowField(true)->save($params);

        if ($result !== false) {
            
            $this->LitestoreGoods->addGoodsSpec($params,$spec_many_params,$spec_a);
           
            $this->success('提交成功');
        } else {
            $this->error($this->model->getError());
        }

    }

    /**
    * 获取出租的分类下的商品列表(Second_hand_shop:二手铺,Rental_house:出租屋,Handcraft:手作坊)
    * @param int $type 传分区过来
    * @param int $category Second_hand_shop,Rental_house,Handcraft 中其中一个
    **/
    public function rent_good_list(){
      $father = Db::name('category')->where(['type'=>input('type'),'flag'=>input('category'),'pid'=>0])->field('id,name')->order('weigh desc')->select();

      if($father){
        foreach ($father as $key => $value) {
            
              $father[$key]['goods_list'] = Db::name('LitestoreGoods')->where(['pid'=>$value['id']])->field('goods_id,goods_name,introduce,show_money')->order('weigh desc')->select();

          }
        }
        
      $this->success('获取出租的分类下的商品列表',$father);
    }
  

}
